package com.wx.controller;

import com.wx.pojo.Cat;
import com.wx.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class UserController {

	//	@InitBinder
//	public void initBinder(WebDataBinder binder) {
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//		dateFormat.setLenient(false);
//		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
//	}


	/**
	 * @param global 全局 @ControllerAdvice 中的属性值 {@link MyControllerAdvice#globalAttribute(Model)}
	 */
	@ModelAttribute
	public void localAttribute(Model model, @ModelAttribute("global") String global) {
		model.addAttribute("local", "局部属性");
		System.out.println("global = " + global);
	}


	/**
	 * @param model 参数 model 中现在有两个属性，分别是如下:
	 * {@link MyControllerAdvice#globalAttribute(Model)}
	 * {@link #localAttribute(Model, String)}
	 */
	@RequestMapping(path = "/saveUser")
	public String saveUser(User user, Model model) {
		model.addAttribute("user", user);
		return "user";
	}


	@RequestMapping(path = "/saveUserAndCat")
	public String saveUserAndCat(User user, Cat cat, Model model) {
		model.addAttribute("user", user);
		model.addAttribute("cat", cat);
		return "user";
	}


	@RequestMapping(path = "/getUser")
	@ResponseBody
	public User getUser() {
		return new User("放弃也是一种选择", "131523", new Date());
	}


	@RequestMapping(path = "/requestBody")
	@ResponseBody
	public void requestBody(@RequestBody String body) {
		System.out.println(body);
	}

	@RequestMapping(path = "/ajax", produces = "text/plain;charset=utf-8")
	@ResponseBody
	public Cat/*String*/ ajax(Cat cat) {
		System.out.println(cat.getName());
		System.out.println(cat.getAge());
		return cat;
		/*return "Hello, 你好！";*/
	}
}
